package com.apes.scm.rbac.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.rbac.model.Menu;
import com.apes.scm.rbac.model.Role;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pengyb
 */
public interface MenuRepository extends CustomRepository<Menu, Integer> {


    List<Menu> findAllByOrderBySortCode();


    List<Menu> findAllByParentIsNullOrderBySortCodeAsc();

    @Query("select  new map(e.id as id,e.name as name,e.path as path,e.valid as valid,e.terminalType as terminal) from Menu e order by e.sortCode")
    List<Map> findTreeData();

     @Query("select T from Menu T where lastUpdatedStamp > :times or (id > :id and lastUpdatedStamp = :times) order by lastUpdatedStamp, id asc ")
     List<Menu> findByLastUpdatedStampAndId(@Param("times") Date times, @Param("id") int id, Pageable pageable);

 /**
     * fetch data by rule Group And Terminal
     *
     * @param group rule group
     * @param terminal rule terminal
     * @return Result List<Map>
     */
    @Query("select  new map(e.id as id,e.name as name,e.path as path,e.valid as valid,e.groupType.description as groupType,e.sortCode as sortCode) from Menu e where e.groupType.id =:group and e.terminalType.id =:terminal order by e.sortCode")
    List<Map> findTreeDataByGroupAndTerminal(@Param("group") String group, @Param("terminal") String terminal);

    /**
     * fetch data by rule terminalTypeId
     *
     * @param terminalTypeId rule terminalTypeId
     * @return Result List<Map>
     */
    @Query("select  new map(e.id as id,e.name as name,e.path as path,e.valid as valid,e.terminalType as terminal ,e.parent.id as parentId) from Menu e where e.terminalType.id=:value order by e.sortCode")
    List<Map> findTreeDataByTerminalTypeId(@Param("value") String terminalTypeId);}
